﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CourseManagerModels;
using CourseManagerBLL;

namespace CourseManagerUI
{
	public partial class FrmAdminLogin : Form
	{
        private TeacherManager teacherManager = new TeacherManager();
		public FrmAdminLogin()
		{
			InitializeComponent();
		}

        #region 窗体移动代码
        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//标签是否为左键
        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }
        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }
        private void Frm_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }


		#endregion

        //关闭窗口按钮
		private void CloseFrmAdminLoginBtn_Click(object sender, EventArgs e)
		{
            this.Close();
		}

		private void LoginButton_Click(object sender, EventArgs e)
		{
			//输入验证
			if (this.UserNameTextBox.Text.Trim().Length == 0)
			{
                MessageBox.Show("请输入登录密码！", "提示信息");
                //选中当前的输入框
                this.UserNameTextBox.Focus();
                return;
            }
            if (this.PassWordTextBox.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入登录密码！", "提示信息");
                this.PassWordTextBox.Focus();
                return;
            }
            //封装用户输入的对象
            Teacher teacher = new Teacher()
            {
                LoginAccount = this.UserNameTextBox.Text.Trim(),
                LoginPwd = this.PassWordTextBox.Text.Trim()
            };

            //调用BLL模块
            teacher = teacherManager.TeacherLogin(teacher);

            //验证登录
            if(teacher != null)
			{
                //如果登录成功，我们首先要保存登录信息（保存到全局变量中，以备后续使用）
                Program.currentTeacher = teacher;
                //设置窗体的返回值
                this.DialogResult = DialogResult.OK;
                this.Close();
			}
			else
			{
                MessageBox.Show("登录账号或密码错误！", "登录提示");
            }

        }
	}
}
